<?php
namespace Xiaoc\Api;


/**
 * Xiaoc商城店铺
 *
 * @author: zhy <zhy302813@163.com> 2017-8-19
 */

class Seller extends Common {
        public function getRules() {
            return array(
                'getSellerInfo' => array(
                    'seller_id'   => array('name' => 'seller_id', 'require' => true,'desc'=>'商家ID','type'=>'int' ),
                    'token'       => array('name'=>'token','desc'=>'用户TOKEN')
                ),
                'getShopInfo' => array(
                    'seller_id'   => array('name' => 'seller_id', 'require' => true,'desc'=>'商家ID','type'=>'int' ),
                    'token'       => array('name'=>'token','desc'=>'用户TOKEN')
                )
            );
	}


      /**
       * 商家店铺信息
      * @desc商家详细信息
       */
      public function getShopInfo(){
           $seller_id = $this->seller_id;
           $prefix = \PhalApi\DI()->config->get('dbs.tables.__default__.prefix');
           $sql = 'SELECT a.shop_id,a.user_id,a.rz_shopName,b.shop_name,b.shop_address,b.shop_logo,b.logo_thumb,b.street_thumb,b.brand_thumb,b.notice,b.street_desc,b.ru_id FROM ' . $prefix.'merchants_shop_information as a JOIN ' . $prefix.'seller_shopinfo as b ON a.user_id=b.ru_id  WHERE a.user_id=' . $seller_id;
           $res = \PhalApi\DI()->notorm->merchants_shop_information->queryRows($sql, array());
           $res = $res[0];
           
           //最新商品
           $list = \PhalApi\DI()
                    ->notorm
                    ->goods
                    ->select('goods_id,goods_name,shop_price,goods_thumb')
                    ->where(array('user_id'=>$res['ru_id'],"is_on_sale"=>1,"is_alone_sale"=>1,'is_delete'=>0))
                    ->order('sort_order, goods_id DESC')
                    ->limit(6)
                    ->fetchAll();
           $res['new_list'] = $list; 

           //热卖商品
           $sql = 'SELECT count(*) sale_num,goods_id,goods_name,shop_price,goods_thumb FROM (SELECT a.goods_id,a.goods_name,a.shop_price,a.goods_thumb FROM ' . $prefix.'goods as a  LEFT JOIN ' . $prefix.'order_goods as b ON a.goods_id=b.goods_id  WHERE a.user_id=' . $res['ru_id'].' AND is_on_sale=1 AND is_alone_sale=1 AND is_delete=0) u  group by goods_id order by sale_num DESC';
           $hot_lists = \PhalApi\DI()->notorm->goods->queryAll($sql, array());
           $res['hot_lists'] = $hot_lists; 

           //关注人数
           $count_gaze = \PhalApi\DI()
                    ->notorm
                    ->collect_store
                    ->select('*')
                    ->where(array('ru_id'=>$seller_id))
                    ->fetchAll();
            $res['count_gaze'] = count($count_gaze);
            //是否已经关注
            $status=false;
            if ($this->token){
                $userinfo = $this->get_users_info($this->token);
                $is_collect = $this->medoo->get('collect_store','rec_id',array('ru_id'=>$seller_id,'user_id'=>$userinfo['user_id']));
                if ($is_collect){
                    $status = true;
                }

            }
            $res['is_collect'] = $status;
            $data['res'] = $res;
            //获取店铺幻灯片
            $banner_where['ru_id'] = $seller_id;
            $banner_where['is_show'] = 1;
            $banner_where['ORDER'] = array('img_order'=>'ASC');
            $data['shop_banner'] = $this->medoo->select('seller_shopslide',array('img_url','img_link','img_desc'),$banner_where);
            return $data;
      }
     
     /**
      * 商家信息
      * @desc商家详细信息
      */
      public function getSellerInfo(){
           $seller_id = $this->seller_id;
           $shopinfo = \PhalApi\DI()
                    ->notorm
                    ->merchants_shop_information
                    ->select('*')
                    ->where(array('user_id'=>$seller_id))
                    ->fetchOne();
            $sellerinfo = \PhalApi\DI()
                    ->notorm
                    ->seller_shopinfo
                    ->select('*')
                    ->where(array('ru_id'=>$seller_id))
                    ->fetchOne();
            $count_gaze = \PhalApi\DI()
                    ->notorm
                    ->collect_store
                    ->select('*')
                    ->where(array('ru_id'=>$seller_id))
                    ->fetchAll();
            //是否已经关注
            $status='';
            // if (0 < $_SESSION['user_id']) {
            //   $status = \PhalApi\DI()
            //         ->notorm
            //         ->collect_store
            //         ->select('rec_id')
            //         ->where(array('ru_id'=>$seller_id,'user_id'=>$_SESSION['user_id']))
            //         ->fetchOne();
            //   $status = (0 < $status ? 'active' : '');
            // }
            // 店铺保证金
  

  

          $prefix = \PhalApi\DI()->config->get('dbs.tables.__default__.prefix');
          $sql = 'SELECT s.grade_name, s.grade_img, s.grade_introduce, s.white_bar, g.grade_id, g.add_time, g.year_num, g.amount FROM ' . $prefix.'seller_grade' . ' AS s ' . 'LEFT JOIN ' . $prefix.'merchants_grade' . ' AS g ON s.id = g.grade_id WHERE ' . 'g.ru_id = \'' . $seller_id . '\' LIMIT 1';
          $res['seller_grade'] = \PhalApi\DI()->notorm->seller_grade->queryRows($sql, array());
          $res['shop_name'] = $sellerinfo['shop_name'];
          $res['shop_title'] = $sellerinfo['shop_title'];
          $res['shop_keyword'] = $sellerinfo['shop_keyword'];
          $res['shop_address'] = $sellerinfo['shop_address'];
          $res['logo_thumb'] = $sellerinfo['logo_thumb'];
          $res['kf_tel'] = $sellerinfo['kf_tel'];
          $res['count_gaze'] = count($count_gaze);
          $res['gaze_status'] = $status;

            if(!empty($shopinfo)){
              $res['shop_name'] = $shopinfo['rz_shopName'];
              $res['shopNameSuffix'] = $shopinfo['shopNameSuffix'];
              $res['shoprz_brandName'] = $shopinfo['shoprz_brandName'];
              $res['self_run'] = $shopinfo['self_run'];
              $res['shop_expireDateStart'] = $shopinfo['shop_expireDateStart'];
            }
            //评分
            $score = \PhalApi\DI()
                        ->notorm
                        ->comment_seller
                        ->select('desc_rank,service_rank,delivery_rank')
                        ->where(array('ru_id'=>$seller_id))
                        ->fetchAll();
             $desc = $serve = $speed = $i = 0;
             foreach($score as $vo){
                 $desc += $vo['desc_rank'];
                 $serve += $vo['service_rank'];
                 $speed += $vo['delivery_rank'];
                 $i++;
             }
             if($i>0){
                $store['score'] = array('desc'=>round($desc/$i,1),'serve'=>round($serve/$i,1),'speed'=>round($speed/$i,1)); 
             }else{
                 $store['score'] = array('desc'=>5.0,'serve'=>5.0,'speed'=>5.0);
             }
              $status=false;
              if ($this->token){
                  $userinfo = $this->get_users_info($this->token);
                  $is_collect = $this->medoo->get('collect_store','rec_id',array('ru_id'=>$seller_id,'user_id'=>$userinfo['user_id']));
                  if ($is_collect){
                      $status = true;
                  }

              }
            $res['is_collect'] = $status;
            $res['cmt'] = $store;
            $data['res'] = $res;
            return $data;
      }
    /**
     * 店铺类型
     * @desc店铺类型
     */
    public function get_shop_type(){
          return array(
              array('code'=>2,'text'=>'专卖店'),
              array('code'=>5,'text'=>'批发市场'),
              array('code'=>6,'text'=>'门店'),
          );
    }
    /**
     * 店铺后缀
     * @desc店铺后缀
     */
    public function get_shop_suffix(){
        return array(
            array('code'=>'专卖店','text'=>'专卖店'),
            array('code'=>'批发市场','text'=>'批发市场'),
            array('code'=>'门店','text'=>'门店'),
        );
    }
        
}
